﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Lavka
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MyExpirePageActionFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);

            filterContext.HttpContext.Response.Expires = -1;
            filterContext.HttpContext.Response.Cache.SetNoServerCaching();
            filterContext.HttpContext.Response.Cache.SetAllowResponseInBrowserHistory(false);
            filterContext.HttpContext.Response.CacheControl = "no-cache";
            filterContext.HttpContext.Response.Cache.SetNoStore();

        }
    }

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{resource}.gif");

            routes.MapRoute(
                "DeleteCartItem", // Route name
                "{controller}/DeleteItem/{id}", // URL with parameters
                new { controller = "Orders", action = "DeleteItem" } // Parameter defaults
            );

            routes.MapRoute(
                "AddToCart", // Route name
                "{controller}/AddToCart/{id}", // URL with parameters
                new { controller = "Products", action = "AddToCart" } // Parameter defaults
            );

            routes.MapRoute(
                "CategoryProducts", // Route name
                "{controller}/Category/{categoryId}", // URL with parameters
                new { controller = "Products", action = "Category" } // Parameter defaults
            );

            routes.MapRoute(
                "BrandProducts", // Route name
                "{controller}/Brand/{brandId}", // URL with parameters
                new { controller = "Products", action = "Brand" } // Parameter defaults
            );
            routes.MapRoute(
                "ProductDetails", // Route name
                "{controller}/Details/{id}", // URL with parameters
                new { controller = "Products", action = "Details" } // Parameter defaults
            );
            routes.MapRoute(
                "ArticleDetails", // Route name
                "{controller}/Details/{id}", // URL with parameters
                new { controller = "Articles", action = "Details" } // Parameter defaults
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/", // URL with parameters
                new { controller = "Home", action = "Index"} // Parameter defaults
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            GlobalFilters.Filters.Add(new MyExpirePageActionFilterAttribute());
        }
    }
}